// BkPtListDialog.cpp : implementation file
//

#include "stdafx.h"
#include "8085.h"
#include "BkPtListDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBkPtListDialog dialog


CBkPtListDialog::CBkPtListDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CBkPtListDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBkPtListDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    m_aRemoved.RemoveAll();
    m_aAddress.RemoveAll();
}


void CBkPtListDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBkPtListDialog)
	DDX_Control(pDX, IDC_BKPT_LIST, m_cBkPtList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBkPtListDialog, CDialog)
	//{{AFX_MSG_MAP(CBkPtListDialog)
	ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
	ON_BN_CLICKED(IDC_BUTTON_REMOVE_ALL, OnButtonRemoveAll)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBkPtListDialog message handlers

BOOL CBkPtListDialog::OnInitDialog() 
{
    CDialog::OnInitDialog();
    OneBreakPoint *pt;
    
    // TODO: Add extra initialization here
    int i=0;
    for(pt=GetDocument()->FirstBreakPoint(); pt ; pt = GetDocument()->NextBreakPoint() )
    {
        char str[128];
        if(pt->m_nSrcLine!=-1)
            sprintf(str,"0x%04x %.10s,  at Line %d",pt->address,
                Get8085Object()->OpcodeToString(pt->address),pt->m_nSrcLine+1);
        else
            sprintf(str,"0x%04x %s",pt->address,
                Get8085Object()->OpcodeToString(pt->address));
        m_cBkPtList.AddString(str);
        m_cBkPtList.SetCheck(i,GetDocument()->IsBkPtEnabled(pt->address));
        m_aAddress.Add(pt->address);
        i++;
    }
    ChangeButtonState();
    return TRUE;  // return TRUE unless you set the focus to a control
        // EXCEPTION: OCX Property Pages should return FALSE
}


void CBkPtListDialog::OnButtonRemove() 
{
	// TODO: Add your control notification handler code here
    int nSel = m_cBkPtList.GetCurSel();
    m_aRemoved.Add(m_aAddress[nSel]);   // add to deleted break points
    m_aAddress.RemoveAt(nSel);  // delete it from the current break points
    m_cBkPtList.DeleteString(nSel);

    // make a new selection
    if(nSel<m_cBkPtList.GetCount())
        m_cBkPtList.SetCurSel(nSel);
    else if(m_cBkPtList.GetCount()!=LB_ERR)
        m_cBkPtList.SetCurSel(m_cBkPtList.GetCount()-1);
    ChangeButtonState();
}

void CBkPtListDialog::OnButtonRemoveAll() 
{
	// TODO: Add your control notification handler code here
    int nCount = m_cBkPtList.GetCount();
    for(int i=0; i<nCount ; i++)
    {
        m_aRemoved.Add(m_aAddress[0]);   // add to deleted break points
        m_aAddress.RemoveAt(0);  // delete it from the current break points
        m_cBkPtList.DeleteString(0);
    }
    ChangeButtonState();
}

BOOL CBkPtListDialog::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
    m_aRemoved.RemoveAll();
    m_aAddress.RemoveAll();
    return CDialog::DestroyWindow();
}

void CBkPtListDialog::ChangeButtonState()
{
    if((m_cBkPtList.GetCount()==LB_ERR)||m_cBkPtList.GetCount()==0)
    {
        // disable the remove buttons
        GetDlgItem(IDC_BUTTON_REMOVE)->EnableWindow(FALSE);
        GetDlgItem(IDC_BUTTON_REMOVE_ALL)->EnableWindow(FALSE);
    }
}

void CBkPtListDialog::OnOK() 
{
	// TODO: Add extra validation here
    // remove the breakpoints from the C8085Object
    int nCount = m_aRemoved.GetSize();
	int i=0;
    for(i=0; i<nCount ; i++) {
		word addr = m_aRemoved[i];
		GetView( )->ToggleBkPtAtAddress( addr );
	}
    nCount = m_aAddress.GetSize();
    for(i=0; i<nCount ; i++)
    {
        word	addr = m_aAddress[i];
		int		bVal = GetDocument()->IsBkPtEnabled(addr);

        if( m_cBkPtList.GetCheck(i) != bVal )
            // item is unchecked - disable breakpoint
			GetView( )->EnableDisableBkPt( addr );
    }
	CDialog::OnOK();
}
